package com.ruoyi.wechat.api;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wechat.domain.CampUser;
import com.ruoyi.wechat.domain.WxLoginUser;
import com.ruoyi.wechat.service.ICampUserService;
import com.ruoyi.wechat.service.IWxLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 微信登录控制器
 * @auth zhaoly
 * @date 2024-01-25 10:16
 */
@Api(value = "微信用户登录", tags = {"微信用户登录"})
@RestController
@RequestMapping("/wechat")
public class WxLoginController extends BaseController {

    @Autowired
    private IWxLoginService loginService;

    @Autowired
    private ICampUserService campUserService;

    /**
     * 小程序登录
     * @param code 微信预登录code
     * @param userName 登录用户名
     * @param password 登录密码
     * @return
     */
    @ApiOperation("小程序用户登录")
    @GetMapping("/login")
    public AjaxResult wxCodeLogin(String code, String userName, String password){
        System.out.println(code);
        WxLoginUser user = loginService.login(code, userName, password);
        return user.getStatus() >= 0 ? success(user) : error(user.getMsg());
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @ApiOperation("小程序信息用户修改")
    @PostMapping("/updateUserInfo")
    public AjaxResult updateUserInfo(@RequestBody CampUser user){
        List<CampUser> list = campUserService.selectCampUserList(user);
        if(list.size() > 0){
            return error("该用户已注册");
        }
        return toAjax(loginService.updateUserInfo(user));
    }

}